package com.android.xunxian.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.xunxian.MyApplication;
import com.android.xunxian.R;
import com.android.xunxian.utils.Util;

import org.xutils.ex.DbException;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by zouqh on 2016/7/23.
 */
public class LuyinActivity extends Activity implements View.OnClickListener {

    @InjectView(R.id.close)
    ImageView close;
    @InjectView(R.id.title_text)
    TextView titleText;
    @InjectView(R.id.ritgh_icon)
    ImageView ritghIcon;
    @InjectView(R.id.ritgh_text)
    TextView ritghText;
    @InjectView(R.id.img)
    ImageView img;
    @InjectView(R.id.state)
    TextView state;
    private String imgPath;
    private String id;
    private String fileName;
    private Dialog Jdialog;

    private boolean flag = true;

    // 录音文件播放
    private MediaPlayer myPlayer;
    // 录音
    private MediaRecorder myRecorder;
    String time;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.luyin_activity);

        ButterKnife.inject(this);
        titleText.setText("录音");
        close.setOnClickListener(this);
        img.setOnClickListener(this);
        id=getIntent().getStringExtra("id");
        myPlayer = new MediaPlayer();
        myRecorder = new MediaRecorder();
        // 从麦克风源进行录音
        myRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
        // 设置输出格式
        myRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
        // 设置编码格式
        myRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        imgPath = Environment.getExternalStorageDirectory().getPath()
                + File.separator + "Xunxian" + File.separator + id + File.separator;

    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close:
                finish();
                break;
            case R.id.img:
                if (flag) {
                    flag = false;
                    state.setText("正在录音...");
                    img.setImageResource(R.mipmap.stop_luy_icon);
                    Date nowTime = new Date();
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
                            "yyyyMMddHHmmss");
                     time = simpleDateFormat.format(nowTime);
                    fileName = id + "_" + time + ".mp3";
                    File saveFilePath = new File(imgPath+fileName);
                    myRecorder.setOutputFile(saveFilePath
                            .getAbsolutePath());
                    try {
                        saveFilePath.createNewFile();
                        myRecorder.prepare();
                        // 开始录音
                        myRecorder.start();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }


                } else {

                    pointDialog("是否保存录音？");
                }


                break;

        }
    }

    /**
     * 提示
     */
    private void pointDialog(String str) {
        LayoutInflater inflaterDl = LayoutInflater.from(this);
        View layout = inflaterDl.inflate(R.layout.prompt_dialog,
                null);
        // 对话框
        Jdialog = new Dialog(this);
        Jdialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        Jdialog.show();

        Jdialog.setContentView(layout);
        TextView sure = (TextView) layout.findViewById(R.id.sureExti);
        TextView cancle = (TextView) layout.findViewById(R.id.cancleExti);
        TextView dialogTitle = (TextView) layout.findViewById(R.id.dialogTitle);
        sure.setText("保存");
        dialogTitle.setText(str);
        sure.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
//


                Intent intent = new Intent();
                intent.putExtra("name", fileName);
                intent.putExtra("time", time);
                setResult(RESULT_OK, intent);
                finish();

                Jdialog.cancel();


            }
        });
        cancle.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (new File(imgPath+fileName).exists()){
                    new File(imgPath+fileName).delete();
                }
                flag = true;
                state.setText("点击录音");
                img.setImageResource(R.mipmap.luyin_icon);
                // 释放资源
//                if (myPlayer.isPlaying()) {
//                    myPlayer.stop();
//                    myPlayer.release();
//                }
//                myPlayer.release();
//                myRecorder.release();
                Jdialog.cancel();
                finish();
            }
        });

    }


    @Override
    protected void onDestroy() {
        // 释放资源
        if (myPlayer.isPlaying()) {
            myPlayer.stop();
            myPlayer.release();
        }
        myPlayer.release();
        myRecorder.release();
        super.onDestroy();
    }
}
